//
//  SGClassificationView.swift
//  songguo
//
//  Created by 潘传洲 on 16/6/16.
//  Copyright © 2016年 pcz. All rights reserved.
//

import UIKit

protocol SGClassificationViewDelegate {
    func chengeFrame()
}

private let bottomViewHeight : CGFloat = 90

// 40 // 90 // 130 400
class SGClassificationView: UIView {
    
    var delegate : SGClassificationViewDelegate!
    
    lazy private var topView : UIView = {
        let height : CGFloat = 40
        let topView = UIView()
        topView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: height)
        
        // 左侧标题
        let textLabel = UILabel()
        textLabel.text = "分类"
        textLabel.textColor = UIColor.color(99, g: 99, b: 99)
        textLabel.font = UIFont.systemFontOfSize(12)
        textLabel.sizeToFit()
        textLabel.x = 5
        textLabel.height = height
        topView.addSubview(textLabel)
        
        // 右侧标题
        let checkAllBtn = UIButton()
        checkAllBtn.addTarget(self, action: #selector(checkAllBtnClick(_ :)), forControlEvents: .TouchUpInside)
        checkAllBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        checkAllBtn.setTitle("查看全部分类", forState: .Normal)
        checkAllBtn.setTitle("收起全部分类", forState: .Selected)
        checkAllBtn.setImage(UIImage(named: "all_type_down_16x16_"), forState: .Normal)
        checkAllBtn.setImage(UIImage(named: "all_type_up_16x16_"), forState: .Selected)
        checkAllBtn.setTitleColor(globalColor, forState: .Normal)
        checkAllBtn.sizeToFit()
        checkAllBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 84, bottom: 0, right: 0)
        checkAllBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -32, bottom: 0, right: 0)
        checkAllBtn.x = screenWidth - checkAllBtn.width - 10
        checkAllBtn.height = height
        topView.addSubview(checkAllBtn)
        
        return topView
    }()
    
    lazy private var bottomView : UIView = {
        let bottomView = UIView()
        bottomView.frame = CGRect(x: 0, y: CGRectGetMaxY(self.topView.frame), width: screenWidth, height: bottomViewHeight)
        let iconArray = ["ts_meishi_72x72_","ts_yinyue_72x72_","ts_lvxing_72x72_","ts_kanpian_72x72_","ts_pinjiu_72x72_","ts_yingxiang_72x72_","ts_fengshang_72x72_","ts_shanghua_72x72_","ts_jujia_72x72_","ts_yundong_72x72_","ts_dushu_72x72_","ts_wanwu_72x72_","ts_yayi_72x72_","ts_sanlian_72x72_"]
        let nameArray = ["美食","音乐","旅行","电影","品酒","摄影","风尚","艺术","家居","运动","读书","玩物","雅艺","周刊"]
        for i in 0..<nameArray.count{
            let maxColNumber = 4
            let row = i % maxColNumber
            let col = i / maxColNumber
            let width : CGFloat = screenWidth / CGFloat(maxColNumber)
            let height : CGFloat = bottomViewHeight
            let x : CGFloat = CGFloat(row) * width;
            let y : CGFloat = CGFloat(col) * height;
            let view = UIView(frame : CGRect(x: x, y: y, width: width, height: height))
            let btn = SGClassificationButton()
            btn.setImage(UIImage(named: iconArray[i]), forState: .Normal)
            btn.setTitle(nameArray[i], forState: .Normal)
            btn.sizeToFit()
            btn.setTitleColor(UIColor.color(99, g: 99, b: 99), forState: .Normal)
            btn.center = CGPoint(x: height / 2, y: width / 2)
            view.addSubview(btn)
            bottomView.addSubview(view)
        }
        return bottomView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(topView)
        self.addSubview(bottomView)
        self.clipsToBounds = true
        self.backgroundColor = UIColor.whiteColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func checkAllBtnClick(btn : UIButton) {
        btn.selected = !btn.selected
        UIView.animateWithDuration(0.25) { 
            self.bottomView.height = btn.selected == false ? bottomViewHeight : 360
        }
        delegate?.chengeFrame()
    }
}
